package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.DtQuery;
import com.ruoyi.project.system.domain.DtRisk;
import com.ruoyi.project.system.mapper.DtQueryMapper;
import com.ruoyi.project.system.mapper.DtRiskMapper;
import com.ruoyi.project.system.service.IDtRiskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.ruoyi.common.constant.CacheConstants.DT_RUN_RISK_NUM;

@RestController
@RequestMapping("/system/risk")
public class DtRiskController extends BaseController {

    @Autowired
    private IDtRiskService dtRiskService;

    @Autowired
    private DtRiskMapper dtRiskMapper;

    @Autowired
    private DtQueryMapper dtQueryMapper;

    @Autowired
    private RedisCache rc;

    @PostMapping("/add")
    public AjaxResult add(@RequestBody DtRisk dtRisk) {
        dtRisk.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dtRiskService.insertDtRisk(dtRisk));
    }

    @Anonymous
    @PostMapping("/three")
    public void threeAdd(@RequestBody DtRisk dtRisk) {
        dtRisk.setDelFlag("1");
        dtRiskService.insertDtRisk(dtRisk);
    }

    @Anonymous
    @GetMapping("/label")
    public AjaxResult riskLabel(String type) {
        return success(dtRiskService.riskLabel(type));
    }

    @Anonymous
    @GetMapping("/pull")
    public AjaxResult pull(@NotNull Integer queryId, @NotBlank String type, @NotBlank String label) {
        return toAjax(dtRiskService.pullBlackHandle(queryId, type, label));
    }

    @Log(title = "风险名单立即查询", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:risk:ture-false-query')")
    @GetMapping("/query")
    public AjaxResult query(DtRisk dtRisk) {
        String key = DT_RUN_RISK_NUM + SecurityUtils.getUserId();
        Integer num = rc.getCacheObject(key);
        Integer limitNUm = rc.getCacheObject(DT_RUN_RISK_NUM + "limitNum");
        if (num == null) {
            rc.setCacheObject(key, 1, 1, TimeUnit.DAYS);
        } else {
            if (num + 1 > limitNUm) {
                return warn("免费查询次数已达每日上限（" + limitNUm + "次/日）");
            }
            rc.setCacheObject(key, ++num);
        }
        boolean nameEmpty = StringUtils.isEmpty(dtRisk.getName());
        boolean cardNoEmpty = StringUtils.isEmpty(dtRisk.getCardNo());
        boolean telEmpty = StringUtils.isEmpty(dtRisk.getTel());
        if (nameEmpty && cardNoEmpty && telEmpty) {
            return error("参数异常！");
        }
        if (!nameEmpty && dtRisk.getName().length() < 2) {
            return error("姓名参数异常！");
        }
        if (!cardNoEmpty && dtRisk.getCardNo().length() < 6) {
            return error("身份证参数异常！");
        }
        if (!telEmpty && dtRisk.getTel().length() < 8) {
            return error("电话号码参数异常！");
        }
        List<DtRisk> dtRisks = dtRiskMapper.queryRiskList(dtRisk);
        return success(dtRisks);
    }

    @Log(title = "历史/审核命中检查", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:risk:ture-false-query')")
    @GetMapping("/check")
    public AjaxResult check(@NotNull Integer queryId) {
        DtQuery dtQuery = dtQueryMapper.selectDtQueryByQueryId(queryId);
        List<DtRisk> dtRisks = dtRiskMapper.queryRiskList(new DtRisk(dtQuery));
        return success(dtRisks);
    }

}
